using System.IO;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public static class AnimatorEditorMenu
    {
        private static string[] GUIDs;

        [MenuItem("Assets/GameEditor/Animation Tool(动画类工具)/关卡->普通实体状态机", false, 1000)]
        static void GenAnimatorLevelEntity()
        {
            GUIDs = Selection.assetGUIDs;
            NextOne(0, ArtModeGenType.LevelEntity);
        }

        [MenuItem("Assets/GameEditor/Animation Tool(动画类工具)/关卡->主角团实体状态机", false, 1000)]
        static void GenAnimatorLevelCharacter()
        {
            GUIDs = Selection.assetGUIDs;
            NextOne(0, ArtModeGenType.LevelCharacter);
        }

        [MenuItem("Assets/GameEditor/Animation Tool(动画类工具)/战斗->英雄状态机", false, 1000)]
        static void GenAnimatorBattleCharacter()
        {
            GUIDs = Selection.assetGUIDs;
            NextOne(0, ArtModeGenType.BattleCharacter);
        }

        [MenuItem("Assets/GameEditor/Animation Tool(动画类工具)/战斗->怪物状态机", false, 1000)]
        static void GenAnimatorBattleMonster()
        {
            GUIDs = Selection.assetGUIDs;
            NextOne(0, ArtModeGenType.BattleMonster);
        }

        private static void NextOne(int index, ArtModeGenType genAnimatorType)
        {
            if (index >= GUIDs.Length)
                return;
            string path = AssetDatabase.GUIDToAssetPath(GUIDs[index]);
            if (!Directory.Exists(path))
            {
                Debug.LogWarning("选择的不是文件夹:" + path);
                return;
            }
            GenAnimatorByType(path, genAnimatorType);
            NextOne(index + 1, genAnimatorType);
        }

        public static string GenAnimatorByType(string path, ArtModeGenType eGenAnimatorType)
        {
            try
            {
                var entityName = Path.GetFileName(path);
                var genereator = AnimatorToolConstant.AniamtorGenerator[(int)eGenAnimatorType];
                if (!genereator.GenInitial(path, entityName))
                {
                    return string.Empty;
                }
                genereator.GenAnimatorController();
                ArtGenRecordUtil.Record(genereator.basePath, genereator.genAnimatorConfig);
                return genereator.GetAnimatorResPath();
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
                return string.Empty;
            }
        }
    }
}
